package com.kodak.collin;

import com.mongodb.*;

public class Sample {
	
	public static void insertChineseCharacterSample() throws Exception{
		Mongo m = new Mongo("localhost", 27017);
		DB db = m.getDB("Demo");
		//insert Chinese Contents into the db.
		DBCollection coll = db.getCollection("chinese");
		
		for(int i =0;i<10000;i++){
			BasicDBObject doc = new BasicDBObject();
			doc.put("name", "张"+i);
			doc.put("age",31);
			coll.insert(doc);
		}
	}
	
	public static void queryChinese() throws Exception{
		Mongo m = new Mongo("localhost", 27017);
		DB db = m.getDB("Demo");
		DBCollection coll = db.getCollection("chinese");
		
		BasicDBObject query = new BasicDBObject();
		query.put("name", "张8888");
		DBCursor cursor = coll.find(query);
		while(cursor.hasNext()){
			System.out.println(cursor.next());
		}
		cursor.close();		
	}

	public static void insertInnerDocument() throws Exception{
		Mongo m = new Mongo("localhost", 27017);
		DB db = m.getDB("Demo");

		
		DBCollection coll = db.getCollection("innerdocument");
		
		for(int i=0;i<100;i++){
			coll.insert(insertSimpleInnerDocument(i));
		}
	}
	
	public static void queryInnerDocument() throws Exception{
		Mongo m = new Mongo("localhost", 27017);
		DB db = m.getDB("Demo");

		
		DBCollection coll2 = db.getCollection("innerdocument");

		DBObject query2 = new BasicDBObject("address.state","CA8");
		DBCursor cursor3 = coll2.find(query2);
		while(cursor3.hasNext()){
			System.out.println(cursor3.next());
		}
		cursor3.close();
	}
	
	public static void insertComplexInnerDocument() throws Exception{
		Mongo m = new Mongo("localhost", 27017);
		DB db = m.getDB("Demo");

		
		DBCollection coll = db.getCollection("ComplexInnerDocument");
		
		for(int i=0;i<100;i++){
			coll.insert(insertComplexInnerDocument(i));
		}
	}
	
	public static void queryComplexInnerDocument() throws Exception{
		Mongo m = new Mongo("localhost", 27017);
		DB db = m.getDB("Demo");
		DBCollection coll = db.getCollection("ComplexInnerDocument");
		DBObject query = new BasicDBObject("address.state","SD8");
		DBCursor cursor3 = coll.find(query);
		while(cursor3.hasNext()){
			System.out.println(cursor3.next());
		}
		cursor3.close();
		
		
		
		
	}
	
	public static void main(String[] args) throws Exception{
		//Sample.insertChineseCharacterSample();
		//Sample.insertInnerDocument();
		//Sample.insertComplexInnerDocument();
		Sample.queryChinese();
		System.out.println("=================================");
		Sample.queryInnerDocument();
		System.out.println("=================================");
		Sample.queryComplexInnerDocument();
	}
	
	/*
	 * Sample Data look like this:
	 * {
	 * 	"name":"someName",
	 * 	"tele":"58841111",
	 * 	"address":[
	 * 				{
	 * 				 "country":"US",
	 * 				 "state"  :"CA",
	 * 				 "city"   :"LA"
	 * 			  	},
	 * 				{
	 * 				 "country":"CN",
	 * 				 "state"  :"SD",
	 * 				 "city"   :"WF"
	 * 			 	}
	 * 			  ]
	 * }
	 * */
	public static BasicDBObject insertComplexInnerDocument(int x){
		BasicDBObject address1 = new BasicDBObject();
		address1.put("country", "US");
		address1.put("state", "CA"+x);
		address1.put("city", "LA");
		
		BasicDBObject address2 = new BasicDBObject();
		address2.put("country", "CN");
		address2.put("state", "SD"+x);
		address2.put("city", "WF");
		
		BasicDBList list = new BasicDBList();
		list.add(address1);
		list.add(address2);
		
		BasicDBObject people = new BasicDBObject();
		people.put("name", "arthur"+x);
		people.put("tele", "55841111");
		people.put("address", list);
		
		return people;	
	}
	
	
	/*
	 * Sample Data look like this:
	 * {
	 * 	"name":"someName",
	 * 	"tele":"58841111",
	 * 	"address":{
	 * 				"country":"US",
	 * 				"state"  :"CA",
	 * 				"city"   :"LA"
	 * 			  }
	 * }
	 * */
	public static BasicDBObject insertSimpleInnerDocument(int x){
		
		BasicDBObject address = new BasicDBObject();
		address.put("country", "US");
		address.put("state", "CA"+x);
		address.put("city", "LA");
		
		BasicDBObject people = new BasicDBObject();
		people.put("name", "arthur"+x);
		people.put("tele", "55841111");
		people.put("address", address);
		
		return people;	
	}


}
